package cn.yikaojiuguo.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.Category;
import cn.yikaojiuguo.student.support.database.MyDBManager;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;
import cn.yikaojiuguo.student.ui.global.HomeActivity;

public class ChooseProvinceActivity extends CustomerActionBarActivity implements View
        .OnClickListener {

    @Bind(R.id.lv)
    ListView mLv;
    private BaseAdapter mAdapter;
    private List<Category> mList;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(), ChooseProvinceActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_province);
        ButterKnife.bind(this);
        setCustomerBackOnclickListener(this);
        mList = MyDBManager.queryByEvalue(ArgsConstants.EVALUE_PROVINCE);
        mAdapter = new ArrayAdapter<Category>(this, android.R.layout.simple_list_item_1,
                mList);
        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                YiKaoApplication.putChoseProvinceIndex(position);
                YiKaoApplication.putProvinceId(mList.get(position).id + "");
                YiKaoApplication.putProvinceName(mList.get(position).ename);
                LogUtil.showInfoLog("ID", "provinceId = " + mList.get(position).id);
                LogUtil.showInfoLog("ID", "majorId = " + YiKaoApplication.getMajorId());
                LogUtil.showInfoLog("ID", "gradeId = " + YiKaoApplication.getGradeId());
                register(mList.get(position).id + "");
            }
        });
    }

    /**
     * 注册
     */
    public void register(String provinceId) {
        showProgressDialogNotCancel();
        String requestUrl = UrlConstants.REGISTER + "&mobile=" + YiKaoApplication.getMobileNubmer
                () + "&code=" + YiKaoApplication.getCaptcha() + "&grade=" + YiKaoApplication
                .getGradeId() + "&province=" + provinceId + "&category=" + YiKaoApplication
                .getMajorId() + "&device=" + ArgsConstants.DEVICE + "&sversion=" +
                YiKaoApplication.getSDKVersion() + "&softversion=" + Util.getVersionName();
        RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                dismissProgressDialog();
                if(response!=null) {
                    if(response.optBoolean("success")) {//注册成功
                        JSONObject data = response.optJSONObject("data");
                        String userId = data.optString("id");
                        String token = data.optString("token");
                        YiKaoApplication.clearCacheUserInfo();
                        //保存user id和token
                        YiKaoApplication.putUserId(userId);
                        YiKaoApplication.putSecretkey(token);
                        startActivity(HomeActivity.newIntent(2));
                        ToastUtil.showToast(ChooseProvinceActivity.this,"注册成功");
                    } else {//注册失败
                        LogUtil.showInfoLog(mTag,"注册失败");
                    }
                }
            }
        },null,mTag);
    }

    @Override
    public void onClick(View v) {
        startActivity(ChooseGradeActivity.newIntent());
        finish();
        leftEnter();
    }

    @Override
    public void onBackPressed() {
        mIv_back.performClick();
    }
}
